package com.itboye.jigongbao.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.itboye.jigongbao.R;
import com.itboye.jigongbao.app.MyApplication;
import com.itboye.jigongbao.bean.CitysBean;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;


/**
 *
 */
public class CityDBManager {

    SQLiteDatabase db;
    Context mContext;
    public static final String DB_NAME = "itboye.db";
    public static final String DB_PATH = MyApplication.Companion.getInstance().getCacheDir() + "/"
           ; // 存放路径;
    private final int BUFFER_SIZE = 400000;
    private Cursor cursor;
    private String cityCode;

    public CityDBManager(Context mContext) {
        this.mContext = mContext;

    }

    /**
     * 被调用方法
     */
    public SQLiteDatabase openDateBase() {
        this.db = this.openDateBase(DB_PATH + "/" + DB_NAME);
        return this.db;
    }

    /**
     * 被调用方法
     */
    public void closeDateBase() {
        if (this.db != null)
            this.db.close();

    }

    /**
     * 打开数据库
     *
     * @param dbFile
     * @return SQLiteDatabase
     * @author sy
     */
    private SQLiteDatabase openDateBase(String dbFile) {
        File file = new File(dbFile);
        if (!file.exists()) {
            // // 打开raw中得数据库文件，获得stream流
            InputStream stream = mContext.getResources().openRawResource(
                    R.raw.itboye);
            try {

                // 将获取到的stream 流写入道data中
                FileOutputStream outputStream = new FileOutputStream(dbFile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = 0;
                while ((count = stream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, count);
                }
                outputStream.close();
                stream.close();
                SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile,
                        null);
                return db;
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return db;
    }

    /***
     * 通过省的ID查找下面的市
     * @param
     * @return
     */
    public ArrayList<CitysBean> getCities(String provinceId) {
        ArrayList<CitysBean> arrayCity = new ArrayList<>();
        db = SQLiteDatabase.openOrCreateDatabase(
                CityDBManager.DB_PATH + "/" + CityDBManager.DB_NAME,
                null);
        if (this.db != null)
            cursor = db.query("common_city", null, "father like ?", new String[]{provinceId + "%"}, null, null, null);
        if (cursor != null)
            while (cursor.moveToNext()) {
                CitysBean citysBean = new CitysBean();
                citysBean.setCityName(cursor.getString(cursor.getColumnIndexOrThrow("city")));
                citysBean.setCityId(cursor.getString(cursor.getColumnIndexOrThrow("cityID")));
                citysBean.setProvinceId(cursor.getString(cursor.getColumnIndexOrThrow("father")));
                citysBean.setType(1);
                arrayCity.add(citysBean);
            }
        cursor.close();
        db.close();
        return arrayCity;
    }

    /***
     * 通过市的ID查找下面的区
     * @param
     * @return
     */
    public ArrayList<CitysBean> getAreas(String cityId) {
        ArrayList<CitysBean> arrayCity = new ArrayList<>();
        db = SQLiteDatabase.openOrCreateDatabase(
                CityDBManager.DB_PATH + "/" + CityDBManager.DB_NAME,
                null);
        if (this.db != null)
            cursor = db.query("common_area", null, "father like ?", new String[]{cityId + "%"}, null, null, null);
        if (cursor != null)
            while (cursor.moveToNext()) {
                CitysBean citysBean = new CitysBean();
                citysBean.setAreaName(cursor.getString(cursor.getColumnIndexOrThrow("area")));
                citysBean.setAreaId(cursor.getString(cursor.getColumnIndexOrThrow("areaID")));
                citysBean.setCityId(cursor.getString(cursor.getColumnIndexOrThrow("father")));
                citysBean.setType(2);
                arrayCity.add(citysBean);
            }
        cursor.close();
        db.close();
        return arrayCity;
    }

    /**
     * 从数据库获取城市数据
     *
     * @return
     */
    public ArrayList<CitysBean> getProvinces() {
        ArrayList<CitysBean> names = new ArrayList<CitysBean>();
        db = SQLiteDatabase.openOrCreateDatabase(
                CityDBManager.DB_PATH + "/" + CityDBManager.DB_NAME,
                null);
        Cursor cursor = db.rawQuery(
                "select * from common_province", null);
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            CitysBean citysBean = new CitysBean();
            citysBean.setProvince(cursor.getString(cursor
                    .getColumnIndex("province")));
            citysBean.setProvinceId(cursor.getString(cursor
                    .getColumnIndex("provinceID")));
            citysBean.setType(0);
            names.add(citysBean);
        }
        return names;
    }
}
